#!/usr/bin/python

import sys

args = {'system': sys.argv[1], 'scenario': sys.argv[2], 'nstates': int(sys.argv[3])}

cleans = list()

print "COASYNTHPL = tools/coasynth.pl framework/printmessages.pl framework/profiles.pl"
print "DERCOAPL = tools/dercoa.pl framework/derive.pl framework/printmessages.pl framework/base.pl framework/primitives.pl"
print "DERPL = tools/der.pl framework/derive.pl framework/printmessages.pl framework/base.pl framework/primitives.pl"
print "DERXPL = tools/derx.pl framework/derive.pl framework/printmessages.pl framework/base.pl framework/primitives.pl"
print "TRANSPL = tools/trans.pl framework/base.pl framework/ca.pl framework/transition.pl framework/printmessages.pl  framework/base.pl framework/primitives.pl"
print "VIEWPL = tools/view.pl framework/ca.pl framework/printmessages.pl"
print "STEP2TEXPL = tools/step2tex.pl framework/base.pl framework/primitives.pl"
print "STATE2TEXPL = tools/state2tex.pl framework/base.pl framework/primitives.pl"
print "COAGRAPHPY = tools/coagraph.py"
print

#all: ana graph

print "all: ana graph"
print

#ana: idemix/out/report

print "ana: {system}/out/report".format(**args)
print

print "Makefile.{system}: genmk.py".format(**args)
print "\t./genmk.py {system} {scenario} {nstates} > Makefile.{system}".format(**args)
print

#idemix/out/report: idemix/out/dercoa12.pl
	#swipl idemix/out/state12.pl $< < idmprops.pl > $@
	
print "{system}/out/report: {system}/out/dercoa{nstates}.pl {scenario}/props.pl".format(**args)
print "\tswipl {system}/out/state{nstates}.pl {system}/out/dercoa{nstates}.pl < {scenario}/props.pl > {system}/out/report".format(**args)
print
cleans.append("{system}/out/report".format(**args))

#idemix/out/der0.pl: idemix/state0.pl
	#swipl $< < tools/der.pl > $@

#idemix/out/der1.pl: idemix/out/state1.pl
	#swipl $< < tools/der.pl > $@

for i in range(0, args['nstates']+1):
	pref="out/" if i>0 else ""
	print "{system}/out/der{i}.pl: {system}/{pref}state{i}.pl $(DERPL)".format(i=i, pref=pref, **args)
	print "\tswipl {system}/{pref}state{i}.pl < tools/der.pl > {system}/out/der{i}.pl".format(i=i, pref=pref, **args)
	print
	cleans.append("{system}/out/der{i}.pl".format(i=i, **args))
	
#idemix/out/dercoa12.pl: idemix/out/state12.pl idmacts.pl
	#swipl $< idmacts.pl < tools/dercoa.pl > $@

print "{system}/out/dercoa{nstates}.pl: {system}/out/state{nstates}.pl {scenario}/acts.pl $(DERCOAPL)".format(**args)
print "\tswipl {system}/out/state{nstates}.pl {scenario}/acts.pl < tools/dercoa.pl > {system}/out/dercoa{nstates}.pl".format(**args)
print
cleans.append("{system}/out/dercoa{nstates}.pl".format(**args))


#idemix/out/state1.pl: idemix/state0.pl idemix/out/der0.pl idemix/step1.pl
	#swipl idemix/state0.pl idemix/out/der0.pl idemix/step1.pl < tools/trans.pl > idemix/out/state1.pl
	
for i in range(1, args['nstates']+1):
	pref="out/" if i-1>0 else ""
	print "{system}/out/state{i}.pl: {system}/{pref}state{im1}.pl {system}/out/der{im1}.pl {system}/step{i}.pl $(TRANSPL)".format(i=i, im1=i-1, pref=pref, **args)
	print "\tswipl {system}/{pref}state{im1}.pl {system}/out/der{im1}.pl {system}/step{i}.pl < tools/trans.pl > {system}/out/state{i}.pl".format(i=i, im1=i-1, pref=pref, **args)
	print
	cleans.append("{system}/out/state{i}.pl".format(i=i, **args))
	
#graph: idemix/out/graph.png
print "graph: {system}/out/graph.png".format(**args)
print

#swipl idemix/out/state12.pl < tools/derx.pl > idemix/out/allder12.pl

print "{system}/out/allder{nstates}.pl: {system}/out/state{nstates}.pl $(DERXPL)".format(**args)
print "\tswipl {system}/out/state{nstates}.pl < tools/derx.pl > {system}/out/allder{nstates}.pl".format(**args)
print
cleans.append("{system}/out/allder{nstates}.pl".format(**args))

#swipl idemix/out/state12.pl idemix/out/allder12.pl  < tools/view.pl > idemix/out/views12.pl

print "{system}/out/views{nstates}.pl: {system}/out/state{nstates}.pl {system}/out/allder{nstates}.pl $(VIEWPL)".format(**args)
print "\tswipl {system}/out/state{nstates}.pl {system}/out/allder{nstates}.pl < tools/view.pl > {system}/out/views{nstates}.pl".format(**args)
print
cleans.append("{system}/out/views{nstates}.pl".format(**args))

#swipl idemix/out/views12.pl idmgraph.pl < tools/coasynth.pl > idemix/out/coalitions

print "{system}/out/coalitions: {system}/out/views{nstates}.pl {scenario}/graph.pl $(COASYNTHPL)".format(**args)
print "\tswipl {system}/out/views{nstates}.pl {scenario}/graph.pl < tools/coasynth.pl > {system}/out/coalitions".format(**args)
print
cleans.append("{system}/out/coalitions".format(**args))

# swipl idemix/out/state12.pl idemix/step1.pl < tools/step2tex.pl > idemix/out/step1.tex

for i in range(1, args['nstates']+1):
  print "{system}/out/step{i}.tex: {system}/out/state{nstates}.pl {system}/step{i}.pl $(STEP2TEXPL)".format(i=i,**args)
  print "\tswipl {system}/out/state{nstates}.pl {system}/step{i}.pl < tools/step2tex.pl > {system}/out/step{i}.tex".format(i=i,**args)
  print
  cleans.append("{system}/out/step{i}.tex".format(i=i,**args))
  
# cat idemix/out/step1.tex ... idemix/out/step12.tex > idemix/out/steps.tex

ins = " ".join(map(lambda i: "{system}/out/step{i}.tex".format(i=i,**args), range(1,args['nstates']+1)))

print "{system}/out/steps.tex: ".format(**args) + ins
print "\tcat " + ins + " > {system}/out/steps.tex".format(**args)
print
cleans.append("{system}/out/steps.tex".format(**args))

# swipl idemix/out/der12.pl idemix/state0.pl < tools/state2tex.pl > idemix/out/state0.tex

print "{system}/out/state0.tex: {system}/out/der{nstates}.pl {system}/state0.pl $(STATE2TEXPL)".format(**args)
print "\tswipl {system}/out/der{nstates}.pl {system}/state0.pl < tools/state2tex.pl > {system}/out/state0.tex".format(**args)
print
cleans.append("{system}/out/state0.tex".format(**args))

for i in range(1, args['nstates']+1):
  print "{system}/out/state{i}.tex: {system}/out/der{nstates}.pl {system}/out/state{i}.pl $(STATE2TEXPL)".format(i=i,**args)
  print "\tswipl {system}/out/der{nstates}.pl {system}/out/state{i}.pl < tools/state2tex.pl > {system}/out/state{i}.tex".format(i=i,**args)
  print
  cleans.append("{system}/out/state{i}.tex".format(i=i,**args))


#tools/coagraph.py idemix/out/coalitions -o idemix/out/graph.png

print "{system}/out/graph.png: {system}/out/coalitions $(COAGRAPHPY)".format(**args)
print "\ttools/coagraph.py {system}/out/coalitions -o {system}/out/graph.png".format(**args)
print
cleans.append("{system}/out/coalitions".format(**args))

print ".DELETE_ON_ERROR:"
print

print "clean:"
for f in cleans:
	print "\trm -f " + f

